﻿namespace SecurityAdministration
{
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class DepartmentInformationBL
    {
        #region Fields

        List<DepartmentFields> _AvailableFields;
        List<DepartmentInformation> _DepartmentList;
        List<DepartmentInformation> _FixedDepartmentList;
        List<DepartmentFields> _StoresDepartmentFields;

        #endregion Fields

        #region Properties

        public List<DepartmentFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentFields>( );
                return _AvailableFields;
            }
        }

        public List<DepartmentInformation> DepartmentList
        {
            get
            {
                if ( _DepartmentList == null )
                    _DepartmentList = new List<DepartmentInformation>( );
                return _DepartmentList;
            }
        }

        public List<DepartmentInformation> FixedDepartmentList
        {
            get
            {
                if ( _FixedDepartmentList == null )
                    _FixedDepartmentList = new List<DepartmentInformation>( );
                return _FixedDepartmentList;
            }
        }

        #endregion Properties

        #region Methods

        public void GetMetaDataFileds( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentFields deptFields = new DepartmentFields( );
                TargetFieldsDataCollectionBL targetFields = new TargetFieldsDataCollectionBL( );

                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList departmentListValues = storeWeb.Lists[ "Stores Detail" ];

                    foreach ( SPField departmentField in departmentListValues.Fields )
                    {
                        if ( !departmentField.Hidden && departmentField.FieldTypeDefinition.UserCreatable )
                        {
                            deptFields = new DepartmentFields( );
                            deptFields.DepartmentName = "Stores Detail";
                            deptFields.FieldName = departmentField.Title.Trim( );
                            deptFields.InternalFieldName = departmentField.InternalName.Trim( );
                            if ( !targetFields.IsFieldSelected( siteURL, targetDepartment, sourceDepartment, deptFields.FieldName ) )
                                AvailableFields.Add( deptFields );
                        }
                    }
                }
            }
        }

        public void GetNonDepartmentFields( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentFields deptFields = new DepartmentFields( );
                TargetFieldsDataCollectionBL targetFields = new TargetFieldsDataCollectionBL( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList currentDepartment = null;
                    SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
                    SPQuery nonUserDepartments = new SPQuery( );

                    nonUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
                    SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
                    if ( otherDepartments.Count > 0 )
                    {
                        foreach ( SPItem departmentItem in otherDepartments )
                        {
                            currentDepartment = storeWeb.Lists[ departmentItem[ "Department List Name" ].ToString( ).Trim( ) ];
                            foreach ( SPField departmentField in currentDepartment.Fields )
                            {
                                if ( !departmentField.Hidden && departmentField.FieldTypeDefinition.UserCreatable )
                                {
                                    deptFields = new DepartmentFields( );
                                    deptFields.DepartmentName = departmentItem[ "Department Name" ].ToString( ).Trim( );
                                    deptFields.FieldName = departmentField.Title.Trim( );
                                    deptFields.InternalFieldName = departmentField.InternalName.Trim( );
                                    if ( !targetFields.IsFieldSelected( siteURL, targetDepartment, sourceDepartment, deptFields.FieldName ) )
                                        AvailableFields.Add( deptFields );
                                }
                            }
                        }
                    }
                    else
                        GetMetaDataFileds( siteURL, sourceDepartment, targetDepartment );
                }
            }
        }

        public void LoadDepartments( string siteURL, bool fixedData )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentInformation singleDepartment = new DepartmentInformation( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
                    SPListItemCollection orderDepts;
                    SPQuery orderDeptGroup = new SPQuery( );
                    orderDeptGroup.Query = "<OrderBy><FieldRef Name='Department_x0020_Name' Ascending='True' /></OrderBy>";
                    orderDepts = departmentListValues.GetItems( orderDeptGroup );
                    foreach ( SPListItem item in orderDepts )
                    {
                        singleDepartment = new DepartmentInformation( );

                        if ( item[ "Department Name" ] != null )
                            singleDepartment.DepartmentName = item[ "Department Name" ].ToString( );
                        if ( item[ "Department List Name" ] != null )
                            singleDepartment.ListName = item[ "Department List Name" ].ToString( );
                        if ( item[ "Department Security Group" ] != null )
                            singleDepartment.SecurityGroup = item[ "Department Security Group" ].ToString( );

                        if ( fixedData )
                            FixedDepartmentList.Add( singleDepartment );
                        else
                            DepartmentList.Add( singleDepartment );
                    }
                    if ( fixedData )
                    {
                        DepartmentInformation standaloneDepartment = new DepartmentInformation( );
                        standaloneDepartment.DepartmentName = "Stores Detail";
                        standaloneDepartment.ListName = "Stores Detail";

                        FixedDepartmentList.Add( standaloneDepartment );
                    }
                }
            }
        }

        #endregion Methods
    }
}